#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：zhcx-service-test 
@File    ：stopproduct_test.py
@Author  ：关依林
@Date    ：2023/10/25 14:22 
'''
import requests
import json
from testcases.Listing_migration.Data_Migration.test_public_way import public_invoking

public_program = public_invoking()

class StopProductDataVerify:
    def __init__(self, startTime, endTime):
        self.startTime = startTime
        self.endTime = endTime

    # 产品变更接口
    def product_change_api(self):
        url = "http://erpapi.zhcxkj.com/erp/products/getdatamonitor"
        payload = json.dumps({
            "customerId": 1,
            "skus": [],
            "fieldTypes": [
                18
            ],
            "startTime": self.startTime,
            "endTime": self.endTime,
            "currentPage": 1,
            "pageSize": 50000
        })
        headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Basic YXBpdXNlcjpFN0kxLWxJX2QjOGRscw=='
        }
        response = requests.request("POST", url, headers=headers, data=payload).json()["data"]["list"]
        temp_list = {}
        for item in response:
            value = temp_list.get(item["sku"], None)
            if value:
                if item["id"] > value[0]:
                    temp_list[item["sku"]] = [item["id"], item["newValue"]]
            else:
                temp_list[item["sku"]] = [item["id"], item["newValue"]]
        return temp_list

    # 获取erp数据
    def get_erp_listing(self, sku):
        sql = f"SELECT sku,OrderSourceSKU,StopProduct FROM RB_Amazon_Product_Base(nolock) WHERE SKU IN {sku} AND Isdelete = 0 AND CustomerID = 1"
        res = public_program.erp_db(sql)
        return res

    # 获取冷库数据
    def get_cold_listing(self, sku):
        sql = f"SELECT sku,OrderSourceSKU,StopProduct FROM aml_amazon_product_base_dep85 WHERE SKU IN {sku} AND Isdelete = 0 AND CustomerID = 1"
        res = public_program.cold_db(sql)
        return res

    # 数据校验
    def data_verify(self):
        value = self.product_change_api()
        sku = tuple(value.keys())
        erp_list = self.get_cold_listing(sku)
        for i in sku:
            data = list(filter(lambda x: x["sku"] == i, erp_list))
            if data:
                listing_value = list(set([str(x["StopProduct"]) for x in data]))
                api_value = [value.get(i)[1]]
                if api_value != listing_value:
                    print(f"SKU:{i},   产品状态为:{api_value[0]},StopProduct状态为:{','.join(listing_value)},  产品状态更新异常...")

if __name__ == '__main__':
    startTime = "2023-11-21 00:00:00"
    endTime = "2023-11-22 07:00:00"
    verify = StopProductDataVerify(startTime,endTime)
    res = verify.data_verify()
